package com.dlmz.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import com.dlmz.util.BaseDao;
import com.dlmz.util.ResultSetToUser;

@SuppressWarnings("serial")
public class LoginJframe extends JFrame{
	//定义组件
	//定义标签
	JLabel jl1,jl2,jl3,jl4,jl5;
	JTextField jtfField;                 //用户名输入框
	JPasswordField jtJPasswordField;     //密码输入框
	JButton jb,jb2;
	public LoginJframe() {
		init();
		listener();
	}
	
	private void listener() {
		jb.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				String username=jtfField.getText().trim();
				String pwd=new String(jtJPasswordField.getPassword()).trim();
				//创建查询的sql语句
				String sql="select * from user where Account=? and Passwd=?";
				//创建BaseDao
				BaseDao baseDao=new BaseDao(); 
				List list=baseDao.query(sql,new ResultSetToUser(),username,pwd); 
				if(list.size()>0) {
					JOptionPane.showMessageDialog(null, "登陆成功"); 
					dispose();
				}else {
					JOptionPane.showMessageDialog(null, "登陆失败"); }	 
			}
		});
		jb2.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				jtfField.setText("");
				jtJPasswordField.setText("");
			}
		});
		jl5.addMouseListener(new MouseListener() {  //注册的响应
			
			@Override
			public void mouseReleased(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mousePressed(MouseEvent e) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void mouseExited(MouseEvent e) {
				// TODO Auto-generated method stub
				jl5.setForeground(Color.black);
			}
			
			@Override
			public void mouseEntered(MouseEvent e) {
				// TODO Auto-generated method stub
				jl5.setForeground(Color.red);
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				// TODO Auto-generated method stub
				dispose();
				new RegistJframe();
			}
		});
		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				int rs=JOptionPane.showConfirmDialog(null, "是否退出系统？");
				if (rs==1) {
					System.exit(0);
				}
			}
		});
	}
	
	public void init() {
		jl1=new JLabel("用户名:");
		jl2=new JLabel("密 码:");
		//jl3=new JLabel(new ImageIcon(bgImage));
		jl4=new JLabel("还没账号？");
		jl5=new JLabel("注册一个");
		//设置jl1的位置
		jl1.setBounds(100,80, 80, 30);
		//设置jl2的位置
		jl2.setBounds(100,140, 100, 30);
		jl4.setBounds(150,270,80,30);
		jl5.setBounds(220,270,60,30);
		//设置jl1的字体
		jl1.setFont(new Font("楷体", Font.BOLD, 20));
		//设置jl2的字体
		jl2.setFont(new Font("楷体", Font.BOLD, 20));
			
		jtfField=new JTextField();
		jtfField.setBounds(180,80,130,30);
		jtJPasswordField=new JPasswordField();
		jtJPasswordField.setBounds(180,140,130,30);
		jb=new JButton("登录");
		jb2=new JButton("重置");
		jb.setBounds(130, 220, 60, 30);
		jb2.setBounds(230, 220, 60, 30);
		this.setLayout(null);
		//将组件添加到窗口中
		this.add(jl1);
		this.add(jtfField);
		this.add(jl2); 
		this.add(jtJPasswordField);
		this.add(jb);
		this.add(jb2);
		this.add(jl4);
		this.add(jl5);
		this.setTitle("登陆界面");
		this.setSize(400,370);
		//设置窗口居中
		this.setLocationRelativeTo(null);
		//设置窗口是否可见
		this.setVisible(true);
	}
	public static void main(String[] args) {
		new LoginJframe();
	}
}
